using System;
using System.Collections.Generic; 
using T.FAS.Runtime.DataAccess.SPI; 
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.FAS.Runtime.Base.Entity;
//using TP.BDP.DCP.Service;
//using TP.BDP.DCP.Entity;
using System.Data;

namespace T.FAS.Runtime.DataAccess.Engine
{
    class BigDataDQQueryService : IModelQueryService<DataQuery>
    {
        public event ExecuteDataAccessAction Execute;
        public event BuildDataAccessActionContext BuildContext;
        //private readonly RequestHandler _handler = new RequestHandler();

        public string GetQueryString(DataQuery dq, Dictionary<string, object> extParam = null)
        {
            try
            {
                return "";
                // return _handler.Check(dq);
            }
            catch (Exception e)
            {
                throw new Exception(string.Format(CommonStringClassEngine.BigDataDQQueryService_GetQueryString,dq.Code,e.Message), e);
            }
        }

        public TResult Query(DataQuery dq, Dictionary<string, object> parameters, Dictionary<string, object> extParam = null)
        {
            try
            {
                //var pars = ConvertDict(parameters);
                //Result result = _handler.Query(dq, pars);
                //var mainQueryResult = result.Data;
                //DataTableCollection allTables = mainQueryResult?.Tables;
                //List<DataTable> listResult = new List<DataTable>();
                //if(allTables != null)
                //{
                //    foreach (DataTable tb in allTables)
                //    {
                //        listResult.Add(tb);
                //    }
                //}
                //int sum = 0;
                //if (mainQueryResult.Tables.Count > 1)
                //    sum = Convert.ToInt32(mainQueryResult.Tables[1].Rows[0][0]);
                //else
                //    sum = mainQueryResult.Tables[0].Rows.Count;
                //var data = DataQueryHelper.BuildTResult(dq, listResult, sum);
                //return data;
                return new TResult();
            }
            catch (Exception e)
            {
                throw new Exception(string.Format(CommonStringClassEngine.BigDataDQQueryService_Query,dq.Code,e.Message), e);
            }
        }

        private Dictionary<string, string> ConvertDict(Dictionary<string, object> parameters)
        {
            Dictionary<string, string> dict = new Dictionary<string, string>();
            foreach (var par in parameters)
            {
                dict[par.Key] = par.Value?.ToString();
            }
            return dict;
        }
    }
}
